/**
  * 面板弹出管理类
  */
module PopUpManager {

    /**
    * 添加面板方法
    * panel       		面板
    * dark        		背景是否变黑
    * popUpWidth      	指定弹窗宽度，定位使用
    * popUpHeight      	指定弹窗高度，定位使用
    * effectType        0：没有动画 1:从中间轻微弹出 2：从中间猛烈弹出  3：从左向右 4：从右向左 5、从上到下 6、从下到上
	* isAlert           动画开关
	* isClick           模态层是否可点击开关
    */
	export function addPopUp(panel: eui.Component, ChildIndex: number = 1, effectType: number = 0, dark: boolean = false, isClick: boolean = true, baseMediator: BaseMediator): void {
		if (panel.parent) {//判断是否包含panel
			PopUpManager.removePopUp(panel, 0, baseMediator)
		}
		var UILayer: eui.UILayer;
		switch (ChildIndex) {
			case 1:
				UILayer = GameLayerManager.gameLayer().sceneLayer
				break;
			case 2:
				UILayer = GameLayerManager.gameLayer().panelLayer
				break;
			case 3:
				UILayer = GameLayerManager.gameLayer().effectLayer
				break;
			case 4:
				UILayer = GameLayerManager.gameLayer().maskLayer
				break;
			case 5:
				UILayer = GameLayerManager.gameLayer().loadLayer
				break;
			default:
				UILayer = GameLayerManager.gameLayer().sceneLayer
				break;
		}
		if (dark) {
			var darkSprite: eui.Rect = new eui.Rect();
			darkSprite.left = 0
			darkSprite.right = 0
			darkSprite.top = 0
			darkSprite.bottom = 0
			darkSprite.fillAlpha = 0.5
			darkSprite.touchEnabled = true;
			UILayer.addChild(darkSprite);
			panel["darkSprite"] = darkSprite;
			//点击模态层，关闭panel
			if (isClick) {
				darkSprite.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
					egret.Tween.removeTweens(panel)
					if (darkSprite.parent) {
						darkSprite.parent.removeChild(darkSprite);
					}
					if (panel.parent) {
						panel.parent.removeChild(panel);
					}
					panel.removeChildren();
				}, this);
			}
		}
		UILayer.addChild(panel);

		var removeTween = function (panel) {
			egret.Tween.removeTweens(panel)
		}
		var popUpWidth = panel.width;
		var popUpHeight = panel.height;
		switch (effectType) {
			case 1:
				panel.alpha = 0;
				panel.scaleX = 0.5;
				panel.scaleY = 0.5;
				panel.x = panel.x + popUpWidth / 4;
				panel.y = panel.y + popUpHeight / 4;
				egret.Tween.get(panel).to({ alpha: 1, scaleX: 1, scaleY: 1, x: panel.x - popUpWidth / 4, y: panel.y - popUpHeight / 4 }, 200, egret.Ease.backOut).call(removeTween, this, [panel]);
				break;
			case 2:
				panel.alpha = 0;
				panel.scaleX = 0.5;
				panel.scaleY = 0.5;
				panel.x = panel.x + popUpWidth / 4;
				panel.y = panel.y + popUpHeight / 4;
				egret.Tween.get(panel).to({ alpha: 1, scaleX: 1, scaleY: 1, x: panel.x - popUpWidth / 4, y: panel.y - popUpHeight / 4 }, 600, egret.Ease.elasticOut).call(removeTween, this, [panel]);
				break;
			case 3:
				panel.x = - popUpWidth;
				egret.Tween.get(panel).to({ x: 0 }, 500, egret.Ease.cubicOut).call(removeTween, this, [panel]);
				break;
			case 4:
				panel.x = popUpWidth;
				egret.Tween.get(panel).to({ x: 0 }, 500, egret.Ease.cubicOut).call(removeTween, this, [panel]);
				break;
			case 5:
				panel.y = - popUpHeight;
				egret.Tween.get(panel).to({ y: 0 }, 500, egret.Ease.cubicOut).call(removeTween, this, [panel]);
				break;
			case 6:
				panel.y = popUpHeight;
				egret.Tween.get(panel).to({ y: 0 }, 500, egret.Ease.cubicOut).call(removeTween, this, [panel]);
				break;
		}
	}

    /**
    * 移除面板方法
    * panel       		面板
    * effectType        0：没有动画 1:从中间缩小消失 2：  3：从左向右 4：从右向左 5、从上到下 6、从下到上
    */
	export function removePopUp(panel: eui.Component, effectType: number = 0, baseMediator: BaseMediator): void {
		if (!panel) {
			return
		}
		let removePanel = function () {
			egret.Tween.removeTweens(panel)
			panel.removeChildren();
			if (panel.parent) {
				panel.parent.removeChild(panel);
			}
			baseMediator.ui = null;
		}
		if (panel["darkSprite"]) {
			if (panel["darkSprite"].parent) {
				panel["darkSprite"].parent.removeChild(panel["darkSprite"]);
			}
		}
		//以下是弹窗动画
		switch (effectType) {
			case 0:
				removePanel();
				break;
			case 1:
				egret.Tween.get(panel).to({ alpha: 0, scaleX: 0, scaleY: 0, x: panel.x + panel.width / 2, y: panel.y + panel.height / 2 }, 300).call(removePanel);
				break;
			case 2:
				removePanel();
				break;
			case 3:
				egret.Tween.get(panel).to({ x: panel.width }, 500, egret.Ease.cubicOut).call(removePanel);
				break;
			case 4:
				egret.Tween.get(panel).to({ x: -panel.width }, 500, egret.Ease.cubicOut).call(removePanel);
				break;
			case 5:
				egret.Tween.get(panel).to({ y: panel.height }, 500, egret.Ease.cubicOut).call(removePanel);
				break;
			case 6:
				egret.Tween.get(panel).to({ y: -panel.height }, 500, egret.Ease.cubicOut).call(removePanel);
				break;
			default:
				break;
		}
	}

}


